package com.eliota.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eliota.controller.dto.DeviceDto;
import com.eliota.entity.Device;
import com.eliota.entity.ProjectDevice;
import com.eliota.mapper.DeviceMapper;
import com.eliota.mapper.ProjectDeviceMapper;
import com.eliota.service.ProjectDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MG
 * @since 2023-04-05
 */
@Service
public class ProjectDeviceServiceImpl extends ServiceImpl<ProjectDeviceMapper, ProjectDevice> implements ProjectDeviceService {

    @Resource
    public ProjectDeviceMapper projectDeviceMapper;

    @Resource
    public DeviceMapper deviceMapper;

    @Override
    public void backDevice(ProjectDevice projectDevice) {

        Device device = new Device();
        //按照归还的设备编号将状态设置为以后以归还状态
        deviceMapper.downBStatus(projectDevice.getDeviceId());
        //pd表的状态改成归还，
        projectDeviceMapper.downStatus(projectDevice.getId());
        //timestamps>>>>>>>
        // 获取当前时间的 LocalDateTime 对象
        LocalDateTime now = LocalDateTime.now();
        /*// 将 LocalDateTime 转换为 Instant 类型并获取时间戳（毫秒级）
        long timestamp = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();*/
        // 将时间戳赋值给对象的 endTime 属性
        projectDeviceMapper.updateEndTime(now,projectDevice.getDeviceId());


    }

    @Override
    public void saveBorrow(DeviceDto deviceDto) {
        /*1.deivce表中借阅状态变更为以借阅
        * 2.projectDevice表中插入设备id和用户id，借阅状态设置1借阅
        * 3.保存当前时间到startTime时间戳*/
        ProjectDevice pd = new ProjectDevice();

        deviceMapper.upBStatus(deviceDto.getId());
        pd.setUserId(deviceDto.getUserId());
        pd.setDeviceId(deviceDto.getId());
        pd.setStatus(1);


        //timestamps>>>>>>>
        // 获取当前时间的 LocalDateTime 对象
        LocalDateTime now = LocalDateTime.now();
        // 将 LocalDateTime 转换为 Instant 类型并获取时间戳（毫秒级）
        long timestamp = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        // 将时间戳赋值给对象的 createTime 属性
        pd.setStartTime(now);

        saveOrUpdate(pd);
    }

    @Override
    public List<ProjectDevice> selectProjectDevice( Map<String, Object> params) {
        return projectDeviceMapper.selectProjectDevice(params);
    }

    @Override
    public void deleteById(Integer id) {
        //归还设备，status = 0
        projectDeviceMapper.downStatus(id);
        //device表同步更新
        QueryWrapper<ProjectDevice> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("id",id);
        ProjectDevice projectDevice = projectDeviceMapper.selectOne(oneWrapper);
        Integer deviceId = projectDevice.getDeviceId();
        deviceMapper.downBStatus(deviceId);
    }

    @Override
    public void deleteBatchByIds(List<Integer> ids) {
        for (Integer id : ids) {
            //归还设备，status = 0
            projectDeviceMapper.downStatus(id);
            //device表同步更新
            QueryWrapper<ProjectDevice> oneWrapper = new QueryWrapper<>();
            oneWrapper.eq("id",id);
            ProjectDevice projectDevice = projectDeviceMapper.selectOne(oneWrapper);
            Integer deviceId = projectDevice.getDeviceId();
            deviceMapper.downBStatus(deviceId);
        }
    }


}
